Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🚀 이슈 번호
Resolve: {#2319}
🧩 문제 해결
스스로 해결: ❌
🔎 접근 과정
사이클 판정과, 시간 복잡도 줄이는 게 중요한 문제였다.
사이클 판정
단순 dfs에다가, 같은 경로 상에서 방문했던 곳을 또 방문하면 사이클이 생성되므로 -1을 return
(처음에는 연결된 부분 그래프 내의 노드라면 동일한 경로가 아니더라도 0,0로 돌아갈 수 있어서 사이클이 생성된다고 판단했는데, 칸별로 이동 거리가 달라서 0,0에서 왔다고 0,0으로 돌아갈 수 있는 것이 아니라는 것을 간과함)
dp
최장 경로기 때문에, 백트레킹이 불가능하게 코드를 짜면 1->2와 1->3->2 중에 전자가 먼저 처리 되고, 후자는 간과됨 -> 따라서 백트래킹이 가능하게 만들어야함
또한 원래는 dpt값을 인자로 받으면서 갱신해 갔지만, 현재 위치에서 갈수있는 경로를 다 탐색 하고 그 중 최대값을 저장하여서, 중복탐색을 막음
⏱️ 시간 복잡도
O(4NM)각 이동에 대해 4개의 이동방향이 있고, dp를 통해서 각 칸이 딱 1번만 계산됨(dp가 없으면 4^MN이 되게 됨.)
💻 구현 코드